package com.tank.tank;

import java.io.*;
import java.util.Vector;

public class Recorder {
    private static int hitenemtycount=0;
    private static String path="D:\\javacode\\javacode\\Tank_3\\score.txt";
    private static  FileWriter fw=null;
    private static BufferedReader br=null;
    private static BufferedWriter bw=null;
    private static Vector<EnemyTank>enemyTanks=new Vector<>();
    private static Vector<Node> nodes=new Vector<>();

    //恢复游戏进度
    public static Vector<Node> getInfo(){
        try {
            br=new BufferedReader(new FileReader(path));
            //读档
            hitenemtycount=Integer.parseInt(br.readLine());
            String line="";
            while((line=br.readLine())!=null){
                String []xyd=line.split(" ");
                Node node=new Node(Integer.parseInt(xyd[0]),Integer.parseInt(xyd[1]),Integer.parseInt(xyd[2]));
                nodes.add(node);//把读档后的Node添加到nodes;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
         return nodes;
    }

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static int getHitenemtycount() {
        return hitenemtycount;
    }

    public static void setHitenemtycount(int hitenemtycount) {
        Recorder.hitenemtycount = hitenemtycount;
    }
    public static void addHitenemtycont(){
        hitenemtycount++;
    }
    public static void keepRecord(){
        try {
            bw=new BufferedWriter(new FileWriter(path));
            bw.write(hitenemtycount+"\r\n");
            for(int i=0;i<enemyTanks.size();i++){
                EnemyTank enemyTank=enemyTanks.get(i);
                if(enemyTank.isLive()){
                    String record=enemyTank.getX()+" "+enemyTank.getY()+" "+enemyTank.getDirect();
                    bw.write(record+"\r\n");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
